package com.xiaozhengkeji.blogsuper.api;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xiaozhengkeji.blogsuper.base.entitys.SystemConfigEntity;
import com.xiaozhengkeji.blogsuper.base.entitys.UserEntity;
import com.xiaozhengkeji.blogsuper.base.service.SystemConfigEntityService;
import com.xiaozhengkeji.blogsuper.base.service.UserEntityService;
import com.xiaozhengkeji.blogsuper.bean.QueryInfo;
import com.xiaozhengkeji.blogsuper.bean.ResponseBean;
import com.xiaozhengkeji.blogsuper.bean.type.ResCodeType;
import com.xiaozhengkeji.blogsuper.utils.page.ListPager;
import com.xiaozhengkeji.blogsuper.utils.page.Pagers;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wiki.xsx.core.util.RedisUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 系统Api
 */
@Slf4j
@RestController
@RequestMapping("/web/system")
public class SystemApi {
    @Autowired
    UserEntityService userEntityService;
    @Autowired
    SystemConfigEntityService systemConfigEntityService;

    /**
     * 添加一个系统配置项
     */
    @ApiOperation("添加一个系统配置项 需要权限(admin:all)")
    @PostMapping("/config/add")
    @SaCheckPermission("admin:all")
    public ResponseBean addConfig(@ApiParam("配置名") @RequestParam("ConfigName") String ConfigName,
                                  @ApiParam("配置值") @RequestParam("ConfigValue") String ConfigValue,
                                  @ApiParam("配置备注") @RequestParam("ConfigRemark") String ConfigRemark) {
        List<SystemConfigEntity> systemConfigEntities = systemConfigEntityService.queryByConfigName(ConfigName);
        if (systemConfigEntities.size() > 0) {
            return new ResponseBean(ResCodeType.Error, "已经存在该系统变量!", null, null);
        }
        SystemConfigEntity temp_SystemConfigEntity = new SystemConfigEntity();
        temp_SystemConfigEntity.setConfigName(ConfigName);
        temp_SystemConfigEntity.setConfigValue(ConfigValue);
        temp_SystemConfigEntity.setRemark(ConfigRemark);
        systemConfigEntityService.insert(temp_SystemConfigEntity);
        return new ResponseBean(ResCodeType.Success, "添加系统变量完成!", null, null);
    }

    /**
     * 获取一个系统变量
     */
    @ApiOperation("获取一个系统变量 需要权限(admin:all)")
    @GetMapping("/config/get")
    @SaCheckPermission("admin:all")
    public ResponseBean getConfig(@ApiParam("配置名") @RequestParam("ConfigName") String ConfigName) {
        List<SystemConfigEntity> systemConfigEntities = systemConfigEntityService.queryByConfigName(ConfigName);
        if (systemConfigEntities.size() > 0) {
            return new ResponseBean(ResCodeType.Success, systemConfigEntities.get(0).getConfigValue(), systemConfigEntities.get(0).getRemark(), null);
        }
        return new ResponseBean(ResCodeType.Error, "不存在此系统变量!", null, null);
    }

    /**
     * 获取全部的系统变量
     */
    @ApiOperation("获取全部的系统变量 - 可用于查询 自动分页 需要权限(admin:all)")
    @PostMapping("/config/getAll")
    @SaCheckPermission("admin:all")
    public ResponseBean getConfigAll(@ApiParam("需要查询的配置名 空为查询全部") @RequestParam("query") String query,
                                     @ApiParam("第几页") @RequestParam("pageNum") Integer pageNum,
                                     @ApiParam("每页数量") @RequestParam("pageSize") Integer pageSize) {
        List<SystemConfigEntity> systemConfigEntities = systemConfigEntityService.queryByAll(new SystemConfigEntity());
        ListPager<SystemConfigEntity> systemConfigEntityListPager = Pagers.newListPager(pageSize, systemConfigEntities);
        return new ResponseBean(ResCodeType.Success, "!", systemConfigEntityListPager.getPage(pageNum), systemConfigEntities.size() / pageSize);
    }

    /**
     * 删除一个系统变量
     */
    @ApiOperation("删除一个系统变量 需要权限(admin:all)")
    @GetMapping("/config/del")
    @SaCheckPermission("admin:all")
    public ResponseBean delConfig(@ApiParam("配置名") @RequestParam("ConfigName") String ConfigName) {
        systemConfigEntityService.deleteByConfigName(ConfigName);
        return new ResponseBean(ResCodeType.Success, "删除系统变量完成!", null, null);
    }

    /**
     * 获取系统详情
     */
    @ApiOperation("获取系统详情 注册用户/文章数量/访问次数 需要权限(admin:all)")
    @GetMapping("/info")
    @SaCheckPermission("admin:all")
    public ResponseBean getInfo() {
        List<UserEntity> userEntities = userEntityService.queryByAll(new UserEntity());
        Long daf = RedisUtil.getNumberHandler(33).getLong("Blog_Access_Frequency");
        HashMap<String, Object> ret = new LinkedHashMap<>();
        ret.put("registerUserSize", userEntities.size());
        ret.put("NumberOfArticles", 0);
        ret.put("TotalNumberOfVisits", daf);
        return new ResponseBean(ResCodeType.Success, "!", ret, null);
    }
}
